package com.xyy.saas.payment.controller;

import com.xyy.saas.payment.cashier.core.bo.CashierGeneralCodeBO;
import com.xyy.saas.payment.cashier.core.bo.CashierPayBO;
import com.xyy.saas.payment.cashier.core.bo.CashierPayInfoBO;
import com.xyy.saas.payment.cashier.core.service.CashierService;
import com.xyy.saas.payment.cashier.core.vo.*;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.api.CashierDeskApi;
import com.xyy.saas.payment.cores.bo.CashierPrePayBO;
import com.xyy.saas.payment.cores.bo.PrepayJumpCashierBo;
import com.xyy.saas.payment.cores.enums.PayModeEnum;
import com.xyy.saas.payment.cores.vo.PrepayJumpCashierVo;
import com.xyy.saas.payment.cores.vo.ResultVO;
import com.xyy.saas.payment.util.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/** 收银台接口 */
@RestController
@RequestMapping("/api/cashier")
public class CashierController {
    private static Logger LOGGER = LoggerFactory.getLogger(CashierController.class);
    @Autowired
    private CashierService cashierService;
    @Resource
    private CashierDeskApi cashierDeskApi;

    /** 收银台-预下单接口 */
    @PostMapping("/prepayJumpCashier")
    public ResultVO<PrepayJumpCashierBo> prepayJumpCashier(@RequestBody PrepayJumpCashierVo prePayVo) {
        return cashierDeskApi.prepayJumpCashier(prePayVo, "");
    }

	/** 收银台-预支付接口(不必关注) */
    @PostMapping("/prepay")
    public ResultVO<CashierPrePayBO> prepay(@RequestBody CashierPrePayVO prePayVo) {
        return ResultVO.createError("当前不支持预支付接口");
    }

	/** 收银台-查看支付信息 */
    @PostMapping("/payinfo")
    public ResultVO<CashierPayInfoBO> payInfo(@RequestBody @Validated  CashierPayInfoVO payInfoVO) {
        try {
            CashierPayInfoBO payInfo = cashierService.payInfo(payInfoVO);
            return ResultVO.createSuccess(payInfo);
        } catch (PaymentException e) {
            LOGGER.error("CashierController#payInfo exception: ", e);
            return e.getCodeEnum() == null ? ResultVO.createError(e.getErrMsg()) :ResultVO.createError(e.getCodeEnum());
        }
    }

	/** 收银台-获取通用码 */
    @PostMapping("/generalcode")
    public ResultVO<CashierGeneralCodeBO> generalcode(@RequestBody @Validated CashierGeneralCodeVO cashierGeneralCodeVO) {
        try {
            CashierGeneralCodeBO cashierGeneralCodeBO = cashierService.generalCode(cashierGeneralCodeVO);
            return ResultVO.createSuccess(cashierGeneralCodeBO);
        } catch (PaymentException e) {
            LOGGER.error("CashierController#payInfo exception: ", e);
            return e.getCodeEnum() == null ? ResultVO.createError(e.getErrMsg()) :ResultVO.createError(e.getCodeEnum());
        }
    }


	/** 收银台-支付接口 */
    @PostMapping("/pay")
    public ResultVO<CashierPayBO> pay(@RequestBody @Validated CashierPayVO payVO, HttpServletRequest requestMsg) {
        String payNo ="";
        try {
            String version = requestMsg.getHeader("version");
            payNo = cashierService.getPayNo(payVO.getPayNo());
            CashierPayBO pay = cashierService.pay(payVO, version);
            LOGGER.info("CashierController#pay end {} payVO:{} pay:{}",payNo, JSONUtils.toJSON(payVO), JSONUtils.toJSON(pay));
            return ResultVO.createSuccess(pay);
        } catch (PaymentException e) {
            LOGGER.error("CashierController#payInfo pay error{}: CashierPayVO: {}",payNo, JSONUtils.toJSON(payVO), e);
            return e.getCodeEnum() == null ? ResultVO.createError(e.getErrMsg()) :ResultVO.createError(e.getCodeEnum());
        }
    }

	/** 收银台-查看支付结果 */
    @PostMapping("/payresult")
    public ResultVO<Boolean> payResult(@RequestBody @Validated PayResultVO payResultVO) {
        try {
            return ResultVO.createSuccess(cashierService.payResult(payResultVO));
        } catch (PaymentException e) {
            LOGGER.error("CashierController#payInfo payResult: PayResultVO: {}", JSONUtils.toJSON(payResultVO), e);
            return e.getCodeEnum() == null ? ResultVO.createError(e.getErrMsg()) :ResultVO.createError(e.getCodeEnum());
        }
    }
}
